package org.hgvc.webapp.ets.spring.dao;

import java.util.Date;
import java.util.List;

import org.hgvc.webapp.ets.bo.Member;
import org.hgvc.webapp.ets.bo.MemberPointEntitle;
import org.hgvc.webapp.ets.bo.RCIAssignment;
import org.hgvc.webapp.ets.bo.RCIAssignmentDetail;
import org.hgvc.webapp.ets.bo.RCIBooking;
import org.hgvc.webapp.ets.bo.RCIBookingEntry;
import org.hgvc.webapp.ets.bo.RCIBulkBank;
import org.hgvc.webapp.ets.bo.RCIIncomingMember;
import org.hgvc.webapp.ets.bo.RCIMember;
import org.hgvc.webapp.ets.bo.RCISplitWeekConfig;
import org.hgvc.webapp.ets.constant.AdminConstant;
import org.hgvc.webapp.ets.constant.BookingConstant;
import org.hgvc.webapp.ets.constant.CommonConstant;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Restrictions;
import org.jocsb.base.AbstractHibernateDao;
import org.jocsb.base.util.DateUtil;


public class RCIHbmDao extends AbstractHibernateDao{
	
	public List<RCIBulkBank> getRCIBulkBankOutgoingList()throws Exception{
		Criteria criteria=getSession().createCriteria(RCIBulkBank.class);
		criteria.add(Restrictions.eq("isDeleted", CommonConstant.NOT_DELETED));
		criteria.add(Restrictions.eq("reservedType", BookingConstant.RCI_TYPE_OUTGOING));
		List<RCIBulkBank> list=criteria.list();
		return list;
	}
	
	public List<RCIBulkBank> getRCIBulkBankList()throws Exception{
		Criteria criteria=getSession().createCriteria(RCIBulkBank.class);
		criteria.add(Restrictions.eq("isDeleted", CommonConstant.NOT_DELETED));
		List<RCIBulkBank> list=criteria.list();
		return list;
	}
	public List<RCIBulkBank> getRCIBulkBankAvList()throws Exception{
		Criteria criteria=getSession().createCriteria(RCIBulkBank.class);
		criteria.add(Restrictions.eq("isDeleted", CommonConstant.NOT_DELETED));
		criteria.add(Restrictions.eq("availableFlag", new Integer(0)));
		List<RCIBulkBank> list=criteria.list();
		return list;
	}
	
	public List<RCIMember> getRCIMemberList()throws Exception{
		Criteria criteria=getSession().createCriteria(RCIMember.class);
		criteria.add(Restrictions.eq("isDeleted", CommonConstant.NOT_DELETED));
		List<RCIMember> list=criteria.list();
		return list;
	}
	
	public List<RCIAssignment> getRCIAssignmentList()throws Exception{
		Criteria criteria=getSession().createCriteria(RCIAssignment.class);
		criteria.add(Restrictions.eq("isDeleted", CommonConstant.NOT_DELETED));
		List<RCIAssignment> list=criteria.list();
		return list;
	}
	
	public List<RCIAssignmentDetail> getRCIAssignmentDetailList(RCIAssignment rciAssignment)throws Exception{
		Criteria criteria=getSession().createCriteria(RCIAssignmentDetail.class);
		criteria.add(Restrictions.eq("rciAssignment.rciAssignmentId", rciAssignment.getRciAssignmentId()));
		criteria.add(Restrictions.eq("isDeleted", CommonConstant.NOT_DELETED));
		List<RCIAssignmentDetail> list=criteria.list();
		return list;
	}
	
	public List<RCIAssignmentDetail> getRCIAssignmentDetailList(RCIBulkBank rciBulkBank)throws Exception{
		Criteria criteria=getSession().createCriteria(RCIAssignmentDetail.class);
		criteria.add(Restrictions.eq("rciBulkBank.rciBulkBankId", rciBulkBank.getRciBulkBankId()));
		criteria.add(Restrictions.eq("isDeleted", CommonConstant.NOT_DELETED));
		List<RCIAssignmentDetail> list=criteria.list();
		return list;
	}
	
	
	public List<RCIAssignment> getRCIAssignmentList(RCIMember rciMember)throws Exception{
		Criteria criteria=getSession().createCriteria(RCIAssignment.class);
		criteria.add(Restrictions.eq("isDeleted", CommonConstant.NOT_DELETED));
		criteria.add(Restrictions.eq("rciMember.rciMemberId", rciMember.getRciMemberId()));
		List<RCIAssignment> list=criteria.list();
		return list;
	}
	
	public List<RCIBulkBank> getRCIBulkBankList(String searchField,String searchKeyword)throws Exception{
		if (searchKeyword==null) searchKeyword="";
		Criteria criteria=getSession().createCriteria(RCIBulkBank.class);
		if (searchField.equalsIgnoreCase(AdminConstant.SEARCH_YEAR) && !searchKeyword.equalsIgnoreCase(""))
			criteria.add(Restrictions.eq("year", new Integer(searchKeyword)));
		if (searchField.equalsIgnoreCase(AdminConstant.SEARCH_PARTNER_CODE)){
			Criteria partnetProductCrt=criteria.createCriteria("partnerProduct");
			Criteria partnetCrt=partnetProductCrt.createCriteria("partner");
			partnetCrt.add(Restrictions.ilike("code", "%"+searchKeyword+"%"));
		}
		if (searchField.equalsIgnoreCase(AdminConstant.SEARCH_PRODUCT_CODE)){
			Criteria partnetProductCrt=criteria.createCriteria("partnerProduct");
			partnetProductCrt.add(Restrictions.ilike("code", "%"+searchKeyword+"%"));
		}
		criteria.add(Restrictions.eq("isDeleted", CommonConstant.NOT_DELETED));
		List<RCIBulkBank> list=criteria.list();
		return list;
	}
	
	public List<RCIBooking> getRCIBookingList(RCIBulkBank rciBulkBank)throws Exception{
		StringBuffer strb = new StringBuffer();
		strb.append(" select distinct rciBookingDetail.rciBooking from RCIBookingDetail rciBookingDetail ");
		strb.append(" where rciBookingDetail.rciBulkBank.rciBulkBankId=:rciBulkBankId");
		strb.append(" and rciBookingDetail.isDeleted=:isDeleted");
		
		Query query = getSession().createQuery(strb.toString());
		query.setParameter("rciBulkBankId", rciBulkBank.getRciBulkBankId());
		query.setParameter("isDeleted", CommonConstant.NOT_DELETED);
		
		List<RCIBooking> list=query.list();
		return list;
	}
	
	public List<RCIAssignment> getRCIAssignmentList(RCIBulkBank rciBulkBank)throws Exception{
		StringBuffer strb = new StringBuffer();
		strb.append(" select distinct rciAssignmentDetail.rciAssignment from RCIAssignmentDetail rciAssignmentDetail ");
		strb.append(" where rciAssignmentDetail.isDeleted=:isDeleted ");
		strb.append(" and rciAssignmentDetail.rciBulkBank.rciBulkBankId=:rciBulkBankId ");
	
		Query query = getSession().createQuery(strb.toString());
		query.setParameter("rciBulkBankId", rciBulkBank.getRciBulkBankId());
		query.setParameter("isDeleted", CommonConstant.NOT_DELETED);
		
		List<RCIAssignment> list = query.list();
		return list;
	}
	
	public List<RCIIncomingMember> getRCIIncomingMemberList()throws Exception{
		StringBuffer strb = new StringBuffer();
		
		strb.append(" select r from RCIIncomingMember r ");
		strb.append(" where r.isDeleted=:isDeleted ");
		
		Query query = getSession().createQuery(strb.toString());
		query.setParameter("isDeleted", CommonConstant.NOT_DELETED);
		
		List<RCIIncomingMember> list = query.list();
		return list;
	}
	
	public List<RCISplitWeekConfig> getRCISplitWeekConfigList()throws Exception{
		StringBuffer strb = new StringBuffer();
		
		strb.append(" select r from RCISplitWeekConfig r ");
		strb.append(" where r.isDeleted=:isDeleted ");
		
		Query query = getSession().createQuery(strb.toString());
		query.setParameter("isDeleted", CommonConstant.NOT_DELETED);
		
		List<RCISplitWeekConfig> list = query.list();
	
		return list;
	}
	
	public List<RCIBookingEntry> getRCIBookingEntryList()throws Exception {
		StringBuffer strb = new StringBuffer();
		
		strb.append(" select r from RCIBookingEntry r ");
		strb.append(" where r.isDeleted=:isDeleted ");
		
		Query query = getSession().createQuery(strb.toString());
		query.setParameter("isDeleted", CommonConstant.NOT_DELETED);
		
		List<RCIBookingEntry> list = query.list();
		return list;
	}
	
	
}
